<?php

namespace Admin\Controller;

use Think\Controller;

class ItemController extends Controller {

     public function __construct() {
        parent::__construct();
        $this->item = new \Admin\Logic\Admin_itemLogic();
    }

    public function _msg($msg){
        exit($msg);
    }


    /*商品列表*/
    public function index(){
        if(empty($_SESSION['user']['id'])){
            $this->_msg('请先登录！');
        }
        $page = (int) $_GET['p'];
        $items = $this->item->getItems($page);
        $this->assign('page',$list['page']);
        $this->assign('list',$list['list']);
        $this->display('goods');
    }

    /*商品创建*/
    public function create(){
    	if(empty($_SESSION['user']['id'])){
            $this->_msg('非法操作！');
        }
        $id = (int)$_GET['id'];
        $item_number = trim($_GET['item_number']);
        if($id>0){
            $item = $this->item->getItemById($item_number);
            if(!$item){
                $this->_msg('该商品不存在！');exit;
            }
            $item['intro'] = str_replace('<br/>', "\n",$item['intro']);
            $item['intro'] = str_replace('<span style=\'color:red\'>', "",$item['intro']);
            $item['intro'] = str_replace('</span>', "",$item['intro']);
            
        }
        //商品分类
        $type = $this->item->type();

        $this->assign('item',$item);
        $this->assign('type',$type);
        $this->display('create');
    }

    public function submit(){
        if($_SESSION['user']['id']<1){
            $this->_msg('非法操作！');
        }
        var_dump($_POST);exit;
        $data['name'] = $_POST['name'];
        $data['title'] = $_POST['title'];
        $data['type_id'] = (int)$_POST['type'];
        $data['price'] = (int)($_POST['price']*100);
        $data['intro'] = json_encode($_POST['intro']);
        $data['image'] = $_POST['image'];
        $data['create_time'] = time();
        $data['status'] = 2;
        //
        $img['weight'] = $_POST['weight'];
        $img['brand'] = $_POST['brand'];
        if(strlen($data['name']) <1 ||
           count($data['image']) <1 ||
           strlen($data['title']) <1 ){
           $this->_msg('_SYSTEM_ERROR_');
        }
        var_dump($data);exit;



        if((int)$_POST['id']){
            // 更新
            $data['item_number'] = $_POST['item_number'];
            $getItem = $this->item->getItemByNumber($data['item_number'],$data['store_id']);
            $result =  array_intersect_assoc($getItem,$data);
            if(count($result) != 10 ){
                // 数据更改时 先删除后插入
                $biaoshi = 1;
            }
            foreach($data['image'] as $k=>$v){
                if($v != $getItem['images'][$k]['image']){
                    $biaoshi = 1;
                }
            }
            if($biaoshi == 1){
                //插入
                $id = $this->item->thingUpdateItem($data,$getItem,$meal,$_item);
                if($id===false){
                    $this->_msg('更新失败!');
                }
            }else{
                //更新
                redirect('/home/goods/preview?item_id='.$getItem['id'].'&store_id='.$data['store_id']);
            }
        }else{
            //添加
            $data['item_number'] = $this->number();
            $id = $this->item->thingInsertItem($data,$arr);
        }
        
                       
        if(!$id){
            $this->_msg('_SYSTEM_ERROR_');
        }else{
            if($_POST['caogao'] == "保存草稿"){
                redirect('/home/goods');
            }
        }
        redirect('/home/goods/preview?item_id='.$id.'&store_id='.$data['store_id']);
    }
	//item_number
    function number() {
        return 'P'. date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }


   //商品删除 js
    public function  delete($item_number){
    	$re = $this->item->delete($item_number);
    	if($re){
    		exit('true');
    	}else{
    		exit('false');
    	}
    	exit;
    }






}